<?php

/**
 * @author martin
 * @copyright 2009
 */

class Lang{
    
    public static $lang = 'Czech';
    public static $phrases = array();
    public static $global = array();
    public static $install = array();
    public static $def_lang = 'Czech';
    
    public function parseComponent($component)
    {
        $path = APPROOT . DIR_SEP . 'language' . DIR_SEP . 'com_' . $component . DIR_SEP . self::$lang . '.ini';
        $def_path =  APPROOT . DIR_SEP . 'language' . DIR_SEP . 'com_' . $component . DIR_SEP . self::$def_lang . '.ini';
        
        if(is_file($path))
        {
            self::$phrases = parse_ini_file($path, true);
        }
        elseif(is_file($def_path))
        {
            self::$phrases = parse_ini_file($def_path, true);
        }
        else
        {
            throw new VException('No language packages found for com_'.$component.' !', 1, __FILE__, __LINE__);
        }
        
    }
    
    public function parseModul($module)
    {
        $path = APPROOT . DIR_SEP . 'language' . DIR_SEP . 'mod_' . $module . DIR_SEP . self::$lang . '.ini';
        $def_path =  APPROOT . DIR_SEP . 'language' . DIR_SEP . 'mod_' . $module . DIR_SEP . self::$def_lang . '.ini';
        
        if(is_file($path))
        {
            self::$phrases = parse_ini_file($path, true);
        }
        elseif(is_file($def_path))
        {
            self::$phrases = parse_ini_file($def_path, true);
        }
        else
        {
            throw new VException('No language packages found for mod_'.$module.' !', 1, __FILE__, __LINE__);
        }
    }
    
    public function getFrontGlobal($text, $variables = array())
    {
        if(empty(self::$global['frontend']))
        {
            self::$global['frontend'] = parse_ini_file(APPROOT . DIR_SEP . 'language' . DIR_SEP . 'global' . DIR_SEP . 'frontend' . DIR_SEP . self::$lang . '.ini');
        }
        
        return self::$global['frontend'][$text];
    }
    
    public function getBackGlobal($text, $variables = array())
    {
        if(empty(self::$global['backend']))
        {
            self::$global['backend'] = parse_ini_file(APPROOT . DIR_SEP . 'language' . DIR_SEP . 'global' . DIR_SEP . 'backend' . DIR_SEP . self::$lang . '.ini');
        }
        
        return self::$global['backend'][$text];
    }
    
    public function getFront($source, $text, $variables = array())
    {
        $type = explode('_', $source, 2);
        
        switch(strtolower($type[0])){
            case 'com':
                self::parseComponent($type[1]);
            break;
            
            case 'mod':
                self::parseModul($type[1]);
            break;
        }
        
        if(!empty($variables)){
            return self::_replace(self::$phrases['frontend'][$text], $variables);
        }else{
            return self::$phrases['frontend'][$text];
        }
    }
    
    public function getBack($source, $text, $variables = array())
    {
        $type = explode('_', $source, 2);
        
        switch(strtolower($type[0])){
            case 'com':
                self::parseComponent($type[1]);
            break;
            
            case 'mod':
                self::parseModul($type[1]);
            break;
        }
        
        if(!empty($variables)){
            return self::_replace(self::$phrases['backend'][$text], $variables);
        }else{
            return self::$phrases['backend'][$text];
        }
    }
    
    public function getInstall($text)
    {
        if(empty(self::$install))
        {
            self::$install = parse_ini_file(APPROOT . DIR_SEP . 'language' . DIR_SEP . 'global' . DIR_SEP . 'install' . DIR_SEP . self::$lang . '.ini');
        }
        
        return self::$install[$text];
    }
    
    private function _replace($str, $args)
    {
        $size = sizeof($args) - 1;
        $search = array();
        for($i = 0; $i <= $size; $i++)
            {
                $search[$i] = '%' . $i;
            }
            
        return str_replace($search, $args, $str);
    }
    
}

?>